package com.github.zxhTom.demo.mina.client;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

/**
 * @author zxh
 */
public class ClientMessageHandler extends IoHandlerAdapter {
    private final static Logger logger = LogManager.getLogger(ClientMessageHandler.class);
    private static final CharsetEncoder charsetEncoder = Charset.forName("UTF-8").newEncoder();
    private static final CharsetDecoder charsetDecoder = Charset.forName("UTF-8").newDecoder();

    public void messageReceived(IoSession session, Object message) throws Exception {
        String msg = (String) message;
        logger.info("=========The message received from Server  is:" + msg);
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
        logger.debug("=========Session Created...");
    }

    @Override
    public void sessionOpened(IoSession session) throws CharacterCodingException {
        for (int i = 0; i < 1; i++) {
            //send(session);
        }
        //我们可以在这里发送一个quit命令，当Server接受到quit命令时，关闭会话
        /*buffer.clear();
        buffer.putString("quit\r\n", charsetEncoder);
        buffer.flip();
        session.write(buffer);*/
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        logger.debug(session + "=========Session Idle...");
        try {
            IoBuffer buffer = IoBuffer.allocate(30);
            buffer.clear();
            buffer.putString("quit\r\n", Charset.forName("utf-8").newEncoder());
            buffer.flip();
            session.write("client send");
        } catch (Exception e) {
            logger.error(e.toString());
        }
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        logger.error(cause.getMessage());
        cause.printStackTrace();
        //session.closeNow();
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        logger.debug("=========messageSent...");
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        logger.debug("=========Session Closed...");
    }

    public void send(IoSession session, String message) {
        try {

            IoBuffer buffer = IoBuffer.allocate(1024);
            buffer.putString(message, charsetEncoder);
            //buffer.putString("@091|20111121114352378|03|15906255640|120.621423,30.920355,34.82,350,2011-11-21 11:43:51,1*3*2|", charsetEncoder);
            buffer.flip();
            session.write(buffer);
        } catch (CharacterCodingException e) {
            e.printStackTrace();
        }

    }
}
